/**
 * 向新手机号码发送验证码
 * tel_num	用户输入的新手机号码
 */
function ajaxGetCodeToNewPhone(tel_num, that, code){
	var url="/Lvscore_User_M/account/ajaxGetCodeToNewPhone";
	$.ajax({
		url:url,
		data:{
			'tel_num' : tel_num,
			'code' : code
		},
		type:"post",
		//timeout:10000,
		success:function(data){
			/**
			 * "0"：服务器异常 发送失败 "1"：发送成功 "-1"：该手机号已被注册
			 */
			that.disabled = false;
			if (data == '1') {
				alertBox("发送成功，请注意查收");
				settime(that);
			} else if (data == '-1'){
				setTimeout(function(){
					validateSpanMsg("telError","该手机号已注册");
					that.removeAttribute("disabled");
					that.value = "获取验证码";
					that.style.opacity = "1";
				},1000);
			} else if (data == '-2'){
				window.location.href = '/Lvscore_User_M/userWeb/views/accounterCenter/oldTel.html';
			} else{
				setTimeout(function(){
					//validateSpanMsg("telError","发送失败，服务器异常");
					alertBox("发送失败，请重试", '');
					that.removeAttribute("disabled");
					that.value = "获取验证码";
					that.style.opacity = "1";
				},1000);
			}
		},
		//error:function(){
		//	alertBox("网络异常，请重试", '');
		//	$("#get").val("获取验证码");
		//},
		dataType:"json"
	});
}
/**
 * 更新平台预留手机号码
 * tel_num	用户输入的手机号码
 */
function ajaxSaveNewPhone(tel_num){
	var url="/Lvscore_User_M/account/ajaxSaveNewPhone";
	$.post(
		url,
		{'tel_num' : tel_num},
		function(data){
			if(data == 1){
				window.location.href="/Lvscore_User_M/userWeb/views/accounterCenter/newTelSucc.html"
			}
		},
		"json"
	);
}

/**
 * 验证新手机验证码是否输入正确
 * code	用户输入的验证码
*/
function ajaxValidateCodeNewPhone(tel, code){
	//loading("数据提交中...");
	var url="/Lvscore_User_M/account/ajaxValidateCodeNewPhone";
	$.ajax({
		url:url,
		data:{
			'tel_num' : tel,
			'code' : code
		},
		type:"post",
		//timeout:10000,
		success:function(data){
			//$(".loadingBox").hide();
			if(data==1){
				var tel=$(".newTel").val();
				ajaxSaveNewPhone(tel);
			}else if(data==-1){
				validateSpanMsg("codeError","验证码输入错误");
			}else{
				alertBox("网络异常，请重试","");
				//validateSpanMsg("codeError","网络异常，请重试");
			}
		},
		//error:function(){
		//	//$(".loadingBox").hide();
		//	alertBox("网络异常，请重试","");
		//	//validateSpanMsg("codeError","服务器异常，请重试");
		//},
		dataType:"json"
	});
}

$(function(){
	$(".newTel").val("");
	$(".code").val("");
	getUrl('accounterCenter/oldTel');
	var code = getQueryString("code");
	var user = getUser_C();
	var oldtel;
	if (user) {
		 oldtel=user.mobile;
	}
	if (!isNotEmpty(code)) {
		window.location.href = '/Lvscore_User_M/userWeb/views/accounterCenter/oldTel.html';
	}
	//实时监控输入框
	inputChange(".newTel","#telError");
	inputChange(".code","#codeError");
	function telMessage(){
		var reg = /^1[3|4|5|7|8]\d{9}$/;
		var tel=$(".newTel").val();
		if(tel==""||tel==null){
			validateSpanMsg("telError","请输入手机号码");
			return false;
		}else if(!reg.test(tel)){
			validateSpanMsg("telError","手机号格式错误");
			return false;
		}else if(tel==oldtel){
			validateSpanMsg("telError","新手机号不能与旧手机号相同");
			return false;
		}else{
			return true;
		}
	}
	function codeMessage(){
		var code=$(".code").val();
		var reg=/^\d{6}$/;
		if(code==null||code==""){
			validateSpanMsg("codeError","请输入验证码");
			return false;
		}else if(!reg.test(code)){
			validateSpanMsg("codeError","验证码错误");
			return false;
		}else{
			return true;
		}
	}
	$(".newTel").blur(function(){
		telMessage();
	})
	$(".code").blur(function(){
		codeMessage();
	})
	//获取验证码
	$("#get").click(function(){
		var tel=$(".newTel").val();
		if(telMessage()){
			$(this).disabled = true;
			$(this).val("发送中...");
			ajaxGetCodeToNewPhone(tel, this, code);
		}
	})
	//点击提交时
	$(".sub").click(function(){
		var tel=$(".newTel").val();
		var code=$(".code").val();
		if(telMessage()&&codeMessage()){
			ajaxValidateCodeNewPhone(tel, code);
		}
	})
})